<?php

namespace App\Services;

use App\Libs\MyTemplateProcessor;
use App\Services\WordToPdfService;
use Log;
use Milon\Barcode\DNS1D;
use App\Services\CommonService;

class ConvertService
{
    public function convertAwardFormDataToWordDocumentOld($taskId, $formData, $awardId, $awardType, $resultPath, $barcode)
    {
        try {
            Log::info("生成word任务开始");
            if ($awardType == 'talent') {
                $templatePath = base_path('vars/docTemplates/talent-template.docx');
            } elseif ($awardType == 'progress') {
                $templatePath = base_path('vars/docTemplates/progress-template.docx');
            } else {
                return '暂无该类型奖项';
            }
            $resultDocFileName = $barcode;
            if ($this->isWindows()) {
                $dir = iconv("UTF-8", "GBK", public_path() . $resultPath);
                if (!file_exists($dir)) {
                    mkdir($dir, 0777, true);
                }
                $templatePath = iconv('UTF-8', 'GBK', $templatePath);
                $resultFilePath = iconv('UTF-8', 'GBK', public_path() . $resultPath . '/' . $resultDocFileName . '.docx');
            } else {
                $dir = public_path() . $resultPath;
                if (!file_exists($dir)) {
                    mkdir($dir, 0777, true);
                }
                $resultFilePath = public_path() . $resultPath . '/' . $resultDocFileName . '.docx';
            }
            $data = json_decode($formData, true);
            $templateProcessor = new MyTemplateProcessor($templatePath);

            //条码
            $barcodeImg = $this->getBarCodeImg($barcode, 'C39', 2, 100);
            $arrImage = array('src' => $barcodeImg, 'size' => [180, 50]);
            $templateProcessor->setImg('barcodeImg', $arrImage);
            $templateProcessor->setValue('barcode', $barcode);

            Log::info("条码图片设置成功");
            //填充模版内容
            if ($awardType == 'talent') {
                //basic 基本信息
                foreach ($data['basic'] as $k => $v) {
                    if ($k == 'job') {
                        $job = $this->setPara($v);
                        $templateProcessor->setValue('basic_' . $k, $job);
                    } elseif ($k == 'situation') {
                        $situation = $this->setPara($v);
                        $templateProcessor->setValue('basic_' . $k, $situation);
                    } else if ($k == 'resume') {
                        $resumeTableCount = count($v);
                        $templateProcessor->cloneRow('resume_range', $resumeTableCount);
                        for ($i = 1; $i <= $resumeTableCount; $i++) {
                            $templateProcessor->setValue('resume_range#' . $i, $v[$i - 1]['range']);
                            $templateProcessor->setValue('resume_address#' . $i, $v[$i - 1]['address'] . ' - ' . $v[$i - 1]['experience']);
                        }
                    } elseif ($k == "document_photo") {
                        $arr = array('src' => public_path($v), 'size' => [110, 150]);
                        $templateProcessor->setImg('person_img', $arr);
                    } else {
                        $templateProcessor->setValue('basic_' . $k, $v);
                    }
                }

                //award 所获科技奖励和荣誉称号情况
                $awardTableCount = count($data['award']);
                $templateProcessor->cloneRow('award_award_name', $awardTableCount);
                for ($i = 1; $i <= $awardTableCount; $i++) {
                    $templateProcessor->setValue('award_award_name#' . $i, $data['award'][$i - 1]['award_name']);
                    $templateProcessor->setValue('award_company_of_issue#' . $i, $data['award'][$i - 1]['company_of_issue']);
                    $templateProcessor->setValue('award_date#' . $i, $data['award'][$i - 1]['date']);
                    $templateProcessor->setValue('award_total#' . $i, $data['award'][$i - 1]['total']);
                    $templateProcessor->setValue('award_rank#' . $i, $data['award'][$i - 1]['rank']);
                }

                //paper 发表论文及专著情况
                $paperTableCount = count($data['paper']);
                $templateProcessor->cloneRow('paper_index', $paperTableCount);
                for ($i = 1; $i <= $paperTableCount; $i++) {
                    $templateProcessor->setValue('paper_index#' . $i, $i);
                    $templateProcessor->setValue('paper_paper_name#' . $i, $data['paper'][$i - 1]['paper_name']);
                    $templateProcessor->setValue('paper_date#' . $i, $data['paper'][$i - 1]['date']);
                    $templateProcessor->setValue('paper_partner#' . $i, $data['paper'][$i - 1]['partner']);
                    $templateProcessor->setValue('paper_rank#' . $i, $data['paper'][$i - 1]['rank']);
                    $templateProcessor->setValue('paper_publish#' . $i, $data['paper'][$i - 1]['publish']);
                    $templateProcessor->setValue('paper_contribution#' . $i, $data['paper'][$i - 1]['contribution']);
                }

                //property 所获知识产权证书
                $propertyTableCount = count($data['property']);
                $templateProcessor->cloneRow('property_index', $propertyTableCount);
                for ($i = 1; $i <= $propertyTableCount; $i++) {
                    $templateProcessor->setValue('property_index#' . $i, $i);
                    $templateProcessor->setValue('property_property_category#' . $i, $data['property'][$i - 1]['property_category']);
                    $templateProcessor->setValue('property_item_name#' . $i, $data['property'][$i - 1]['item_name']);
                    $templateProcessor->setValue('property_country#' . $i, $data['property'][$i - 1]['country']);
                    $templateProcessor->setValue('property_authorization_num#' . $i, $data['property'][$i - 1]['authorization_num']);
                    $templateProcessor->setValue('property_authorization_date#' . $i, $data['property'][$i - 1]['authorization_date']);
                    $templateProcessor->setValue('property_certificate_number#' . $i, $data['property'][$i - 1]['certificate_number']);
                    $templateProcessor->setValue('property_obligee#' . $i, $data['property'][$i - 1]['obligee']);
                    $templateProcessor->setValue('property_inventor#' . $i, $data['property'][$i - 1]['inventor']);
                    $templateProcessor->setValue('property_achievement#' . $i, $data['property'][$i - 1]['achievement']);
                    $templateProcessor->setValue('property_reference_number#' . $i, $data['property'][$i - 1]['reference_number']);
                }

                //scientific 承担科研项目情况
                $scientificTableCount = count($data['scientific']);
                $templateProcessor->cloneRow('scientific_index', $scientificTableCount);
                for ($i = 1; $i <= $scientificTableCount; $i++) {
                    $templateProcessor->setValue('scientific_index#' . $i, $i);
                    $templateProcessor->setValue('scientific_project_name#' . $i, $data['scientific'][$i - 1]['project_name']);
                    $templateProcessor->setValue('scientific_development_date#' . $i, $data['scientific'][$i - 1]['development_date']);
                    $templateProcessor->setValue('scientific_expenditure#' . $i, $data['scientific'][$i - 1]['expenditure']);
                    $templateProcessor->setValue('scientific_project_origin#' . $i, $data['scientific'][$i - 1]['project_origin']);
                    $templateProcessor->setValue('scientific_project_number#' . $i, $data['scientific'][$i - 1]['project_number']);
                    $templateProcessor->setValue('scientific_status#' . $i, $data['scientific'][$i - 1]['status']);
                    $templateProcessor->setValue('scientific_principal#' . $i, $data['scientific'][$i - 1]['principal']);
                    $templateProcessor->setValue('scientific_sort#' . $i, $data['scientific'][$i - 1]['sort']);
                    $templateProcessor->setValue('scientific_reference_number#' . $i, $data['scientific'][$i - 1]['reference_number']);
                }

                //engineering 参与重大工程技术项目情况
                $engineeringTableCount = count($data['engineering']);
                $templateProcessor->cloneRow('engineering_project_name', $engineeringTableCount);
                for ($i = 1; $i <= $engineeringTableCount; $i++) {
//                    $templateProcessor->setValue('engineering_index#' . $i, $i);
                    $templateProcessor->setValue('engineering_project_name#' . $i, $data['engineering'][$i - 1]['project_name']);
                    $templateProcessor->setValue('engineering_issue_company#' . $i, $data['engineering'][$i - 1]['issue_company']);
                    $templateProcessor->setValue('engineering_aboutme#' . $i, $data['engineering'][$i - 1]['aboutme']);
                    $templateProcessor->setValue('engineering_economic#' . $i, $data['engineering'][$i - 1]['economic']);
                }

                //situation 科技成果转化及推广情况
                $situation = $this->setPara($data['situation']);
                $templateProcessor->setValue('situation', $situation);
            } elseif ($awardType == 'progress') {
                $templateProcessor->setValue('this_year', date('Y'));
                //basic_situation 基本情况
                foreach ($data['basic_situation'] as $k => $v) {
                    if ($k == 'plans') {
                        $plans = $this->setPara($v);
                        $templateProcessor->setValue('basic_situation_' . $k, $plans);
                    } elseif ($k == 'technical') {
                        $technical = $this->setPara($v);
                        $templateProcessor->setValue('basic_situation_' . $k, $technical);
                    } elseif ($k == 'develop_domain') {
                        $domain = is_array($v) ? join('；', $v) : (string)$v;
                        $technical = $this->setPara($domain);
                        $templateProcessor->setValue('basic_situation_' . $k, $technical);
                    } elseif ($k == 'task_origin') {
                        $origin = is_array($v) ? join('；', $v) : (string)$v;
                        $technical = $this->setPara($origin);
                        $templateProcessor->setValue('basic_situation_' . $k, $technical);
                    } else {
                        $templateProcessor->setValue('basic_situation_' . $k, $v);
                    }
                }

                //scientific 科技成果简介
                $templateProcessor->setValue('scientific', $this->setPara($data['scientific']));

                //innovate 主要科技创新
                $templateProcessor->setValue('innovate', $this->setPara($data['innovate']));

                //evaluate 客观评价
                $templateProcessor->setValue('evaluate', $this->setPara($data['evaluate']));

                //generalize 推广应用情况
                $templateProcessor->setValue('generalize_situation', $this->setPara($data['generalize']['situation']));
                $templateProcessor->setValue('generalize_description', $this->setPara($data['generalize']['description']));
                $templateProcessor->setValue('generalize_other_description', $this->setPara($data['generalize']['other_description']));
                $templateProcessor->setValue('generalize_society_benefit', $this->setPara($data['generalize']['society_benefit']));
                $economicBenefitTableCount = count($data['generalize']['economic_benefit']);
                $templateProcessor->cloneRow('economic_benefit_year', $economicBenefitTableCount);
                $lj1 = 0;
                $lj2 = 0;
                $lj3 = 0;
                $lj4 = 0;
                for ($i = 1; $i <= $economicBenefitTableCount; $i++) {
                    $templateProcessor->setValue('economic_benefit_year#' . $i, $data['generalize']['economic_benefit'][$i - 1]['year']);
                    $templateProcessor->setValue('economic_finish_sale#' . $i, $data['generalize']['economic_benefit'][$i - 1]['finish_sale']);
                    $templateProcessor->setValue('economic_finish_profit#' . $i, $data['generalize']['economic_benefit'][$i - 1]['finish_profit']);
                    $templateProcessor->setValue('economic_other_sale#' . $i, $data['generalize']['economic_benefit'][$i - 1]['other_sale']);
                    $templateProcessor->setValue('economic_other_profit#' . $i, $data['generalize']['economic_benefit'][$i - 1]['other_profit']);
                    $lj1 += intval($data['generalize']['economic_benefit'][$i - 1]['finish_sale']);
                    $lj2 += intval($data['generalize']['economic_benefit'][$i - 1]['finish_profit']);
                    $lj3 += intval($data['generalize']['economic_benefit'][$i - 1]['other_sale']);
                    $lj4 += intval($data['generalize']['economic_benefit'][$i - 1]['other_profit']);
                }
                $templateProcessor->setValue('finish_sale_lj', $lj1);
                $templateProcessor->setValue('finish_profit_lj', $lj2);
                $templateProcessor->setValue('other_sale_lj', $lj3);
                $templateProcessor->setValue('other_profit_lj', $lj4);

                //knowledge_property 主要知识产权证明目录
                $knowledgePropertyTableCount = count($data['knowledge_property']);
                $templateProcessor->cloneRow('knowledge_property_type', $knowledgePropertyTableCount);
                for ($i = 1; $i <= $knowledgePropertyTableCount; $i++) {
                    $templateProcessor->setValue('knowledge_property_type#' . $i, $data['knowledge_property'][$i - 1]['type']);
                    $templateProcessor->setValue('knowledge_property_name#' . $i, $data['knowledge_property'][$i - 1]['name']);
                    $templateProcessor->setValue('knowledge_property_country#' . $i, $data['knowledge_property'][$i - 1]['country']);
                    $templateProcessor->setValue('knowledge_property_authorization_num#' . $i, $data['knowledge_property'][$i - 1]['authorization_num']);
                    $templateProcessor->setValue('knowledge_property_authorization_date#' . $i, $data['knowledge_property'][$i - 1]['authorization_date']);
                    $templateProcessor->setValue('knowledge_property_certificate_number#' . $i, $data['knowledge_property'][$i - 1]['certificate_number']);
                    $templateProcessor->setValue('knowledge_property_obligee#' . $i, $data['knowledge_property'][$i - 1]['obligee']);
                    $templateProcessor->setValue('knowledge_property_inventor#' . $i, $data['knowledge_property'][$i - 1]['inventor']);
                    $templateProcessor->setValue('knowledge_property_status#' . $i, $data['knowledge_property'][$i - 1]['status']);
                }

                //finish_people 主要完成人情况表
                $completePeopleCount = count($data['finish_people']);
                if ($completePeopleCount > 0) {
                    $templateProcessor->myCloneBlock('PEOPLE_BLOCK', $completePeopleCount);
                    for ($i = 0; $i < $completePeopleCount; $i++) {
                        $tempCompletePeopleData = $data['finish_people'][$i];
                        foreach ($tempCompletePeopleData as $k => $v) {
                            if ($k == 'contribution') {
                                $contribution = $this->setPara($v);
                                $templateProcessor->setValue('finish_people_' . $k, $contribution, 1);
                            } elseif ($k == 'award_situation') {
                                $award_situation = $this->setPara($v);
                                $templateProcessor->setValue('finish_people_' . $k, $award_situation, 1);
                            } else {
                                $templateProcessor->setValue('finish_people_' . $k, $v, 1);
                            }
                        }
                    }
                }
                //finish_company 主要完成单位情况
                $completeCompanyCount = count($data['finish_company']);
                if ($completeCompanyCount > 0) {
                    $templateProcessor->myCloneBlock('COMPANY_BLOCK', $completeCompanyCount);
                    for ($i = 0; $i < $completeCompanyCount; $i++) {
                        $tempCompleteCompanyData = $data['finish_company'][$i];
                        foreach ($tempCompleteCompanyData as $k => $v) {
                            if ($k == 'contribution') {
                                $contribution = $this->setPara($v);
                                $templateProcessor->setValue('finish_company_' . $k, $contribution, 1);
                            } else {
                                $templateProcessor->setValue('finish_company_' . $k, $v, 1);
                            }
                        }
                    }
                }
            }
            $templateProcessor->saveAs($resultFilePath);
            unlink($barcodeImg);
            Log::info("word文件生成成功");
            //生成pdf
            Log::info("word文件转成pdf任务开始");
            $wordToPdfSvc = new WordToPdfService;
            $error = $wordToPdfSvc->convertWordToPdf($resultFilePath, $resultPath, $awardId, $taskId);
            if (empty($error)) {
                Log::info("word文件转成pdf任务成功");
                return null;
            } else {
                Log::error("word文件转成pdf任务失败");
                return $error;
            }
        } catch (\Exception $e) {
            Log::error("出错：" . $e->getMessage());
            return $e->getMessage();
        }
    }

    public function convertAwardFormDataToWordDocument($taskId, $formData, $awardId, $awardType, $resultPath, $barcode)
    {
        try {
            Log::info("生成word任务开始");
            if ($awardType == 'talent') {
                $templatePath = base_path('vars/docTemplates/talent-template.docx');
            } elseif ($awardType == 'progress') {
                $templatePath = base_path('vars/docTemplates/progress-template.docx');
            } else {
                return '暂无该类型奖项';
            }
            $resultDocFileName = $barcode;
            if ($this->isWindows()) {
                $dir = iconv("UTF-8", "GBK", public_path() . $resultPath);
                if (!file_exists($dir)) {
                    mkdir($dir, 0777, true);
                }
                $templatePath = iconv('UTF-8', 'GBK', $templatePath);
                $resultFilePath = iconv('UTF-8', 'GBK', public_path() . $resultPath . '/' . $resultDocFileName . '.docx');
            } else {
                $dir = public_path() . $resultPath;
                if (!file_exists($dir)) {
                    mkdir($dir, 0777, true);
                }
                $resultFilePath = public_path() . $resultPath . '/' . $resultDocFileName . '.docx';
            }
            $data = json_decode($formData, true);
            //填充模版内容
            if ($awardType == 'talent') {
                $this->fillTalentTemplateData($templatePath, $barcode, $data, $resultFilePath);
            } elseif ($awardType == 'progress') {
                $this->fillProgressTemplateData($templatePath, $barcode, $data, $resultFilePath);
            }
            Log::info("word文件生成成功");

            if ($taskId) {
                //生成pdf
                Log::info("word文件转成pdf任务开始");
                $wordToPdfSvc = new WordToPdfService;
                $error = $wordToPdfSvc->convertWordToPdf($resultFilePath, $resultPath, $awardId, $taskId);
                if (empty($error)) {
                    Log::info("word文件转成pdf任务成功");
                    return null;
                } else {
                    Log::error("word文件转成pdf任务失败");
                    return $error;
                }
            }

        } catch (\Exception $e) {
            Log::error("出错：" . $e->getMessage());
            return $e->getMessage();
        }
    }

    public function fillTalentTemplateData($templatePath, $barcode, $data, $resultFilePath)
    {
        $templateProcessor = new MyTemplateProcessor($templatePath);
        //条码
        $barcodeImg = $this->getBarCodeImg($barcode, 'C39', 2, 100);
        $arrImage = array('src' => $barcodeImg, 'size' => [180, 50]);
        $templateProcessor->setImg('barcodeImg', $arrImage);
        $templateProcessor->setValue('barcode', $barcode);
        Log::info("条码图片设置成功");

        //basic 基本信息
        foreach ($data['basic'] as $k => $v) {
            if ($k == 'job') {
                $job = $this->setPara($v);
                $templateProcessor->setValue('basic_' . $k, $job);
            } elseif ($k == 'situation') {
                $situation = $this->setPara($v);
                $templateProcessor->setValue('basic_' . $k, $situation);
            } else if ($k == 'resume') {
                $resumeTableCount = count($v);
                $templateProcessor->cloneRow('resume_range', $resumeTableCount);
                for ($i = 1; $i <= $resumeTableCount; $i++) {
                    $templateProcessor->setValue('resume_range#' . $i, $v[$i - 1]['range']);
                    $templateProcessor->setValue('resume_address#' . $i, $v[$i - 1]['address'] . ' - ' . $v[$i - 1]['experience']);
                }
            } elseif ($k == "document_photo") {
                $arr = array('src' => public_path($v), 'size' => [110, 150]);
                $templateProcessor->setImg('person_img', $arr);
            } else {
                $templateProcessor->setValue('basic_' . $k, $v);
            }
        }

        //award 所获科技奖励和荣誉称号情况
        $awardTableCount = count($data['award']);
        $templateProcessor->cloneRow('award_award_name', $awardTableCount);
        for ($i = 1; $i <= $awardTableCount; $i++) {
            $templateProcessor->setValue('award_award_name#' . $i, $data['award'][$i - 1]['award_name']);
            $templateProcessor->setValue('award_company_of_issue#' . $i, $data['award'][$i - 1]['company_of_issue']);
            $templateProcessor->setValue('award_date#' . $i, $data['award'][$i - 1]['date']);
            $templateProcessor->setValue('award_total#' . $i, $data['award'][$i - 1]['total']);
            $templateProcessor->setValue('award_rank#' . $i, $data['award'][$i - 1]['rank']);
        }

        //paper 发表论文及专著情况
        $paperTableCount = count($data['paper']);
        $templateProcessor->cloneRow('paper_index', $paperTableCount);
        for ($i = 1; $i <= $paperTableCount; $i++) {
            $templateProcessor->setValue('paper_index#' . $i, $i);
            $templateProcessor->setValue('paper_paper_name#' . $i, $data['paper'][$i - 1]['paper_name']);
            $templateProcessor->setValue('paper_date#' . $i, $data['paper'][$i - 1]['date']);
            $templateProcessor->setValue('paper_partner#' . $i, $data['paper'][$i - 1]['partner']);
            $templateProcessor->setValue('paper_rank#' . $i, $data['paper'][$i - 1]['rank']);
            $templateProcessor->setValue('paper_publish#' . $i, $data['paper'][$i - 1]['publish']);
            $templateProcessor->setValue('paper_contribution#' . $i, $data['paper'][$i - 1]['contribution']);
        }

        //property 所获知识产权证书
        $propertyTableCount = count($data['property']);
        $templateProcessor->cloneRow('property_index', $propertyTableCount);
        for ($i = 1; $i <= $propertyTableCount; $i++) {
            $templateProcessor->setValue('property_index#' . $i, $i);
            $templateProcessor->setValue('property_property_category#' . $i, $data['property'][$i - 1]['property_category']);
            $templateProcessor->setValue('property_item_name#' . $i, $data['property'][$i - 1]['item_name']);
            $templateProcessor->setValue('property_country#' . $i, $data['property'][$i - 1]['country']);
            $templateProcessor->setValue('property_authorization_num#' . $i, $data['property'][$i - 1]['authorization_num']);
            $templateProcessor->setValue('property_authorization_date#' . $i, $data['property'][$i - 1]['authorization_date']);
            $templateProcessor->setValue('property_certificate_number#' . $i, $data['property'][$i - 1]['certificate_number']);
            $templateProcessor->setValue('property_obligee#' . $i, $data['property'][$i - 1]['obligee']);
            $templateProcessor->setValue('property_inventor#' . $i, $data['property'][$i - 1]['inventor']);
            $templateProcessor->setValue('property_achievement#' . $i, $data['property'][$i - 1]['achievement']);
            $templateProcessor->setValue('property_reference_number#' . $i, $data['property'][$i - 1]['reference_number']);
        }

        //scientific 承担科研项目情况
        $scientificTableCount = count($data['scientific']);
        $templateProcessor->cloneRow('scientific_index', $scientificTableCount);
        for ($i = 1; $i <= $scientificTableCount; $i++) {
            $templateProcessor->setValue('scientific_index#' . $i, $i);
            $templateProcessor->setValue('scientific_project_name#' . $i, $data['scientific'][$i - 1]['project_name']);
            $templateProcessor->setValue('scientific_development_date#' . $i, $data['scientific'][$i - 1]['development_date']);
            $templateProcessor->setValue('scientific_expenditure#' . $i, $data['scientific'][$i - 1]['expenditure']);
            $templateProcessor->setValue('scientific_project_origin#' . $i, $data['scientific'][$i - 1]['project_origin']);
            $templateProcessor->setValue('scientific_project_number#' . $i, $data['scientific'][$i - 1]['project_number']);
            $templateProcessor->setValue('scientific_status#' . $i, $data['scientific'][$i - 1]['status']);
            $templateProcessor->setValue('scientific_principal#' . $i, $data['scientific'][$i - 1]['principal']);
            $templateProcessor->setValue('scientific_sort#' . $i, $data['scientific'][$i - 1]['sort']);
            $templateProcessor->setValue('scientific_reference_number#' . $i, $data['scientific'][$i - 1]['reference_number']);
        }

        //engineering 参与重大工程技术项目情况
        $engineeringTableCount = count($data['engineering']);
        $templateProcessor->cloneRow('engineering_project_name', $engineeringTableCount);
        for ($i = 1; $i <= $engineeringTableCount; $i++) {
            $templateProcessor->setValue('engineering_project_name#' . $i, $data['engineering'][$i - 1]['project_name']);
            $templateProcessor->setValue('engineering_issue_company#' . $i, $data['engineering'][$i - 1]['issue_company']);
            $templateProcessor->setValue('engineering_aboutme#' . $i, $data['engineering'][$i - 1]['aboutme']);
            $templateProcessor->setValue('engineering_economic#' . $i, $data['engineering'][$i - 1]['economic']);
        }

        //situation 科技成果转化及推广情况
        $situation = $this->setPara($data['situation']);
        $templateProcessor->setValue('situation', $situation);
        $templateProcessor->saveAs($resultFilePath);
        unlink($barcodeImg);
    }

    public function fillProgressTemplateData($templatePath, $barcode, $data, $resultFilePath)
    {
        $templateProcessor = new MyTemplateProcessor($templatePath);
        //条码
        $barcodeImg = $this->getBarCodeImg($barcode, 'C39', 2, 100);
        $arrImage = array('src' => $barcodeImg, 'size' => [180, 50]);
        $templateProcessor->setImg('barcodeImg', $arrImage);
        $templateProcessor->setValue('barcode', $barcode);
        Log::info("条码图片设置成功");

        $templateProcessor->setValue('this_year', date('Y'));
        //basic_situation 基本情况
        foreach ($data['basic_situation'] as $k => $v) {
            if ($k == 'plans') {
                $plans = $this->setPara($v);
                $templateProcessor->setValue('basic_situation_' . $k, $plans);
            } elseif ($k == 'technical') {
                $technical = $this->setPara($v);
                $templateProcessor->setValue('basic_situation_' . $k, $technical);
            } elseif ($k == 'develop_domain') {
                $domain = is_array($v) ? join('；', $v) : (string)$v;
                $technical = $this->setPara($domain);
                $templateProcessor->setValue('basic_situation_' . $k, $technical);
            } elseif ($k == 'task_origin') {
                $origin = is_array($v) ? join('；', $v) : (string)$v;
                $technical = $this->setPara($origin);
                $templateProcessor->setValue('basic_situation_' . $k, $technical);
            } else {
                $templateProcessor->setValue('basic_situation_' . $k, $v);
            }
        }

        //scientific 科技成果简介
        $templateProcessor->setValue('scientific', $this->setPara($data['scientific']));

        //innovate 主要科技创新
        $templateProcessor->setValue('innovate', $this->setPara($data['innovate']));

        //evaluate 客观评价
        $templateProcessor->setValue('evaluate', $this->setPara($data['evaluate']));

        //generalize 推广应用情况
        $templateProcessor->setValue('generalize_situation', $this->setPara($data['generalize']['situation']));
        $templateProcessor->setValue('generalize_description', $this->setPara($data['generalize']['description']));
        $templateProcessor->setValue('generalize_other_description', $this->setPara($data['generalize']['other_description']));
        $templateProcessor->setValue('generalize_society_benefit', $this->setPara($data['generalize']['society_benefit']));
        $economicBenefitTableCount = count($data['generalize']['economic_benefit']);
        $templateProcessor->cloneRow('economic_benefit_year', $economicBenefitTableCount);
        $lj1 = 0;
        $lj2 = 0;
        $lj3 = 0;
        $lj4 = 0;
        for ($i = 1; $i <= $economicBenefitTableCount; $i++) {
            $templateProcessor->setValue('economic_benefit_year#' . $i, $data['generalize']['economic_benefit'][$i - 1]['year']);
            $templateProcessor->setValue('economic_finish_sale#' . $i, $data['generalize']['economic_benefit'][$i - 1]['finish_sale']);
            $templateProcessor->setValue('economic_finish_profit#' . $i, $data['generalize']['economic_benefit'][$i - 1]['finish_profit']);
            $templateProcessor->setValue('economic_other_sale#' . $i, $data['generalize']['economic_benefit'][$i - 1]['other_sale']);
            $templateProcessor->setValue('economic_other_profit#' . $i, $data['generalize']['economic_benefit'][$i - 1]['other_profit']);
            $lj1 += intval($data['generalize']['economic_benefit'][$i - 1]['finish_sale']);
            $lj2 += intval($data['generalize']['economic_benefit'][$i - 1]['finish_profit']);
            $lj3 += intval($data['generalize']['economic_benefit'][$i - 1]['other_sale']);
            $lj4 += intval($data['generalize']['economic_benefit'][$i - 1]['other_profit']);
        }
        $templateProcessor->setValue('finish_sale_lj', $lj1);
        $templateProcessor->setValue('finish_profit_lj', $lj2);
        $templateProcessor->setValue('other_sale_lj', $lj3);
        $templateProcessor->setValue('other_profit_lj', $lj4);

        //knowledge_property 主要知识产权证明目录
        $knowledgePropertyTableCount = count($data['knowledge_property']);
        $templateProcessor->cloneRow('knowledge_property_type', $knowledgePropertyTableCount);
        for ($i = 1; $i <= $knowledgePropertyTableCount; $i++) {
            $templateProcessor->setValue('knowledge_property_type#' . $i, $data['knowledge_property'][$i - 1]['type']);
            $templateProcessor->setValue('knowledge_property_name#' . $i, $data['knowledge_property'][$i - 1]['name']);
            $templateProcessor->setValue('knowledge_property_country#' . $i, $data['knowledge_property'][$i - 1]['country']);
            $templateProcessor->setValue('knowledge_property_authorization_num#' . $i, $data['knowledge_property'][$i - 1]['authorization_num']);
            $templateProcessor->setValue('knowledge_property_authorization_date#' . $i, $data['knowledge_property'][$i - 1]['authorization_date']);
            $templateProcessor->setValue('knowledge_property_certificate_number#' . $i, $data['knowledge_property'][$i - 1]['certificate_number']);
            $templateProcessor->setValue('knowledge_property_obligee#' . $i, $data['knowledge_property'][$i - 1]['obligee']);
            $templateProcessor->setValue('knowledge_property_inventor#' . $i, $data['knowledge_property'][$i - 1]['inventor']);
            $templateProcessor->setValue('knowledge_property_status#' . $i, $data['knowledge_property'][$i - 1]['status']);
        }

        //finish_people 主要完成人情况表
        $completePeopleCount = count($data['finish_people']);
        if ($completePeopleCount > 0) {
            $templateProcessor->myCloneBlock('PEOPLE_BLOCK', $completePeopleCount);
            for ($i = 0; $i < $completePeopleCount; $i++) {
                $tempCompletePeopleData = $data['finish_people'][$i];
                foreach ($tempCompletePeopleData as $k => $v) {
                    if ($k == 'contribution') {
                        $contribution = $this->setPara($v);
                        $templateProcessor->setValue('finish_people_' . $k, $contribution, 1);
                    } elseif ($k == 'award_situation') {
                        $award_situation = $this->setPara($v);
                        $templateProcessor->setValue('finish_people_' . $k, $award_situation, 1);
                    } else {
                        $templateProcessor->setValue('finish_people_' . $k, $v, 1);
                    }
                }
            }
        }
        //finish_company 主要完成单位情况
        $completeCompanyCount = count($data['finish_company']);
        if ($completeCompanyCount > 0) {
            $templateProcessor->myCloneBlock('COMPANY_BLOCK', $completeCompanyCount);
            for ($i = 0; $i < $completeCompanyCount; $i++) {
                $tempCompleteCompanyData = $data['finish_company'][$i];
                foreach ($tempCompleteCompanyData as $k => $v) {
                    if ($k == 'contribution') {
                        $contribution = $this->setPara($v);
                        $templateProcessor->setValue('finish_company_' . $k, $contribution, 1);
                    } else {
                        $templateProcessor->setValue('finish_company_' . $k, $v, 1);
                    }
                }
            }
        }
        $templateProcessor->saveAs($resultFilePath);
        unlink($barcodeImg);
    }

    public function getBarCodeImg($barCode, $type, $width = 2, $height = 100)
    {
        return DNS1D::getBarcodePNGPath($barCode, $type, $width, $height);
    }

    public function setPara($data)
    {
        $ps = explode("\n", $data);
        $n = count($ps);
        if ($n == 1) {
            return $data;
        }
        $value = '  ';
        for ($i = 0; $i < $n; $i++) {
            $ps[$i] = '<![CDATA[' . $ps[$i] . ']]>';
            if ($i == 0) {
                $value = $value . $ps[$i] . '</w:t></w:r></w:p>';
            } elseif ($i < $n - 1) {
                $value = $value . '<w:p><w:pPr><w:spacing w:line="220" w:lineRule="atLeast"/><w:ind w:firstLineChars="200" w:firstLine="420"/><w:rPr><w:rFonts w:asciiTheme="minorEastAsia" w:eastAsiaTheme="minorEastAsia" w:hAnsiTheme="minorEastAsia"/><w:sz w:val="21"/><w:szCs w:val="21"/></w:rPr></w:pPr><w:r><w:t>' . $ps[$i] . '</w:t></w:r></w:p>';
            } else {
                $value = $value . '<w:p>><w:pPr><w:spacing w:line="220" w:lineRule="atLeast"/><w:ind w:firstLineChars="200" w:firstLine="420"/><w:rPr><w:rFonts w:asciiTheme="minorEastAsia" w:eastAsiaTheme="minorEastAsia" w:hAnsiTheme="minorEastAsia"/><w:sz w:val="21"/><w:szCs w:val="21"/></w:rPr></w:pPr><w:r><w:t>' . $ps[$i];
            }
        }
        return $value;
    }

    public function isWindows()
    {
        return stripos(PHP_OS, 'WIN') === 0;
    }

    public function uuid()
    {
        mt_srand((double)microtime() * 10000);//optional for php 4.2.0 and up.
        $charid = strtoupper(md5(uniqid(rand(), true)));
        $hyphen = chr(45);// "-"
        $uuid = substr($charid, 0, 8) . $hyphen
            . substr($charid, 8, 4) . $hyphen
            . substr($charid, 12, 4) . $hyphen
            . substr($charid, 16, 4) . $hyphen
            . substr($charid, 20, 12);
        return $uuid;
    }

}